package com.simba.server.commands.impl.record;

import java.sql.Timestamp;
import com.simba.common.command.ICommand;
import com.simba.common.database.DbOperator;
import com.simba.server.base.CommonUtil;
import com.simba.server.components.SortProtocolCreator;
import com.simba.server.components.data.SortInfo;

public class RecordPacketOnCmd implements ICommand
{
	public static final String NAME = "RecordPacketOnCmd";
	private final SortInfo sortInfo;
	private final String sql = "insert into tt_packet_on_info(barcode,sorterId,plcId,length,width,height,volume,weight,packetOnTime)"
			+ " values(?,?,?,?,?,?,?,?,?);";

	public RecordPacketOnCmd(SortInfo sortInfo)
	{
		this.sortInfo = sortInfo;
	}

	@Override
	public String getName()
	{
		return RecordPacketOnCmd.NAME;
	}

	@Override
	public Boolean run(Object params)
	{
		Boolean ret = false;

		if (!(params instanceof DbOperator))
		{
			return ret;
		}

		try
		{
			DbOperator dbOperator = (DbOperator) params;

			dbOperator.prepareStatement(this.sql);

			dbOperator.setString(1,  CommonUtil.doNullData(this.sortInfo.getBarcode(), "none"));
			dbOperator.setString(2,  CommonUtil.doNullData(this.sortInfo.getSorterId(), "none"));
			dbOperator.setString(3,  CommonUtil.doNullData(this.sortInfo.getPlcId(), "none"));
			dbOperator.setString(4,  CommonUtil.doNullData(this.sortInfo.getLength(), "0"));
			dbOperator.setString(5,  CommonUtil.doNullData(this.sortInfo.getWidth(), "0"));
			dbOperator.setString(6,  CommonUtil.doNullData(this.sortInfo.getHeight(), "0"));
			dbOperator.setString(7,  CommonUtil.doNullData(this.sortInfo.getVolume(), "0"));
			dbOperator.setString(8,  CommonUtil.doNullData(this.sortInfo.getWeight(), "0"));

			Timestamp packetOnTime = this.sortInfo.getTimestampPacketOn();
			if (packetOnTime == null)
			{
				packetOnTime = SortProtocolCreator.getInstance().createCurrentTimeStamp();
			}
			String packetOnTimeContent = packetOnTime.toString();
			dbOperator.setString(9, packetOnTimeContent);

			int res = dbOperator.executeUpdate();
			if (res > 0)
			{
				ret = true;
			}
			else
			{
				ret = false;
			}
		}
		catch (Exception ex)
		{
			ret = false;
		}

		return ret;
	}

}
